package com.cangchu.util;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件操作类，主要是进行原生IO操作
 * Created by Administrator on 2016/10/28.
 */
public class FileUtils {

    private Context context;

    public FileUtils(Context context) {
        this.context = context;
    }

    public void save(String fileName, String fileContent) throws IOException {
        FileOutputStream out = null;
        try {
            out = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            out.write(fileContent.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    public String read(String fileName) throws IOException {
        FileInputStream in = context.openFileInput(fileName);
        byte[] temp = new byte[1024];
        StringBuilder sb = new StringBuilder("");
        int len = 0;
        while ((len = in.read(temp)) > 0) {
            sb.append(new String(temp, 0, len));
        }
        in.close();
        return sb.toString();
    }
}
